<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">



<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css">


  <meta name="keywords" content="夏鱼的鱼">








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0">






<meta name="description" content="实现原理: MySQL支持单项、异步复制，复制过程中一个服务器充当主服务器，而一个或多个其他服务器充当从服务器。  MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改（更新、删除等等）  每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存更新。   实现步骤： Master将改变记录到二进制日志（binary log）中 Slave将Master的binary log ev">
<meta name="keywords" content="技术博客,摄影,后端,前端">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL主从服务器部署">
<meta property="og:url" content="http://alexskywin.gitee.com/2019/08/15/20190815MySQL主从服务器部署/index.html">
<meta property="og:site_name" content="夏鱼的鱼">
<meta property="og:description" content="实现原理: MySQL支持单项、异步复制，复制过程中一个服务器充当主服务器，而一个或多个其他服务器充当从服务器。  MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改（更新、删除等等）  每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存更新。   实现步骤： Master将改变记录到二进制日志（binary log）中 Slave将Master的binary log ev">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://alexskywin.gitee.com/images/post20190815/8f7e2a97ec247fadc24443e5f6a58c466e0.jpg">
<meta property="og:updated_time" content="2019-08-15T16:00:05.262Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="MySQL主从服务器部署">
<meta name="twitter:description" content="实现原理: MySQL支持单项、异步复制，复制过程中一个服务器充当主服务器，而一个或多个其他服务器充当从服务器。  MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改（更新、删除等等）  每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存更新。   实现步骤： Master将改变记录到二进制日志（binary log）中 Slave将Master的binary log ev">
<meta name="twitter:image" content="http://alexskywin.gitee.com/images/post20190815/8f7e2a97ec247fadc24443e5f6a58c466e0.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>







  <title> MySQL主从服务器部署 | 夏鱼的鱼 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">夏鱼的鱼</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle">身体和灵魂必须有一个在路上的地方</p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://alexskywin.gitee.com/2019/08/15/20190815MySQL主从服务器部署/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="夏鱼的鱼">
    <meta itemprop="description" content>
    <meta itemprop="image" content="/images/avatar.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="夏鱼的鱼">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="夏鱼的鱼" src>
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                MySQL主从服务器部署
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-08-15T23:37:53+08:00">
                2019-08-15
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-08-16T00:00:05+08:00">
                2019-08-16
              </time>
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/服务器部署/" itemprop="url" rel="index">
                    <span itemprop="name">服务器部署</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="实现原理"><a href="#实现原理" class="headerlink" title="实现原理:"></a>实现原理:</h2><ul>
<li><p>MySQL支持单项、异步复制，复制过程中一个服务器充当主服务器，而一个或多个其他服务器充当从服务器。</p>
</li>
<li><p>MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改（更新、删除等等）</p>
</li>
<li><p>每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存更新。</p>
</li>
</ul>
<h2 id="实现步骤："><a href="#实现步骤：" class="headerlink" title="实现步骤："></a>实现步骤：</h2><ul>
<li>Master将改变记录到二进制日志（<strong>binary log</strong>）中</li>
<li>Slave将Master的<strong>binary log events</strong>拷贝到它的中继日志（relay log）</li>
<li>Slave重做<strong>中继日志</strong>的时间，将改变反映它自己的数据。</li>
</ul>
<img src="/images/post20190815/8f7e2a97ec247fadc24443e5f6a58c466e0.jpg" class="full-image">

<h2 id="场景配置："><a href="#场景配置：" class="headerlink" title="场景配置："></a>场景配置：</h2><p>准备两台机器，IP为</p>
<ul>
<li>192.168.0.130</li>
<li>192.168.0.131</li>
</ul>
<p>在130服务器上打开my.cnf（MySQL的配置文件），找到相关binlog配置：</p>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">log-bin</span> = mysql-bin <span class="comment">#binlog开启</span></span><br><span class="line"><span class="attr">binlog_format</span> = mixed <span class="comment">#binlog的格式</span></span><br><span class="line"><span class="attr">server-id</span> = <span class="number">130</span> <span class="comment"># 架构中的服务器唯一值</span></span><br><span class="line"><span class="attr">expire_logs_days</span> = <span class="number">10</span></span><br><span class="line"><span class="attr">early-plugin-load</span> = <span class="string">""</span></span><br></pre></td></tr></table></figure>

<p>配置完成，重启Master Mysql服务器。连接我们的MySQL服务器运行命令查看服务器状态</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">show master status;</span><br><span class="line"></span><br><span class="line">-- 了解一下</span><br><span class="line">-- reset master;可以重新开始我们的binlog</span><br></pre></td></tr></table></figure>

<p>这样，我们的Master主机上配置已经是OK了。</p>
<p>我们配置我们的131服务器上的从MySQL服务器配置，同样进入主机，我们打开my.cnf（MySQL的配置文件）</p>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#log-bin = mysql-bin #注释关闭binlog日志</span></span><br><span class="line"><span class="attr">server-id</span> = <span class="number">131</span></span><br><span class="line"><span class="attr">relay_log</span> = mysql-relay-bin</span><br><span class="line"><span class="attr">expire_logs_days</span> = <span class="number">10</span></span><br><span class="line"><span class="attr">early-plugin-load</span> = <span class="string">""</span></span><br></pre></td></tr></table></figure>

<p>重启我们的Slave上的MySQL服务器。</p>
<p>完成上面操作，我们需要在Master MySQL中给Slave MySQL数据库授权，在我们Master上连接MySQL服务器。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">-- 我允许slave@192.168.0.131这台机器 通过 mysql密码 访问当前的Master</span><br><span class="line">grant replication slave on *.* to slave@192.168.0.131 identified by &apos;mysql密码&apos;</span><br></pre></td></tr></table></figure>

<p>主机已经配置完成。我们切换到我们的Slave主机，配置我们的Slave监听哪一台机器。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">stop slave;</span><br><span class="line"></span><br><span class="line">change master to</span><br><span class="line">master_host=&apos;192.168.0.130&apos;,</span><br><span class="line">master_port=3306,</span><br><span class="line">master_user=&apos;slave&apos;,</span><br><span class="line">master_password=&apos;主机mysql密码&apos;,</span><br><span class="line">master_log_file=&apos;mysql-bin.000001&apos;,</span><br><span class="line">master_log_pos=xxx;</span><br><span class="line">-- master_log_file、master_log_pos 通过Master的show master status;查看</span><br><span class="line"></span><br><span class="line">start slave; -- 开启slave服务器</span><br><span class="line"></span><br><span class="line">show slave status\G</span><br><span class="line"></span><br><span class="line">-- 查看Slave_IO_Running: Yes、Slave_SQL_Running: Yes 这两项就行</span><br></pre></td></tr></table></figure>

<p>这里我们的主从设置已经完成，我们只需要在Master主机器上进行MySQL的操作就能检测我们的主从是否正常的进行复制了。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/14/20190814新的开始/" rel="next" title="2019年新的开始">
                <i class="fa fa-chevron-left"></i> 2019年新的开始
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="夏鱼的鱼">
          <p class="site-author-name" itemprop="name">夏鱼的鱼</p>
          <p class="site-description motion-element" itemprop="description">身体和灵魂必须有一个在路上的地方</p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">2</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">2</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/110177012" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://github.com/NeilyoSummer" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#实现原理"><span class="nav-number">1.</span> <span class="nav-text">实现原理:</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实现步骤："><span class="nav-number">2.</span> <span class="nav-text">实现步骤：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#场景配置："><span class="nav-number">3.</span> <span class="nav-text">场景配置：</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">夏鱼的鱼</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  

  
      <!-- UY BEGIN -->
      <script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid="></script>
      <!-- UY END -->
  




  
  

  

  

  

  


</body>
</html>
